package com.ybear.patsdk.utils;

import android.content.Context;
import android.os.Build;
import android.os.VibrationEffect;
import android.os.Vibrator;

import androidx.annotation.NonNull;
import androidx.core.util.Consumer;

public class Utils {
    private static long firstTime = 0;

    /**
     * 开始震动
     * @param context       上下文
     * @param repeat        循环次数。-1：仅一次
     * @param timings       [0]：震动延迟、[1]：震动时长、[2]：震动延迟、[3]：震动时长...
     */
    public static void startVibrator(@NonNull Context context, int repeat, long... timings) {
        Vibrator v = (Vibrator) context.getSystemService( Context.VIBRATOR_SERVICE );
        if( v == null ) return;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            v.vibrate( VibrationEffect.createWaveform( timings, repeat ) );
        }else {
            v.vibrate( timings, repeat );
        }
    }


    public static void doubleTouch(long time, Consumer<Boolean> call) {
        long secondTime = System.currentTimeMillis();
        if( secondTime - firstTime > time ) {
            firstTime = secondTime;
            //第一次通知
            if( call != null ) call.accept( false );
        } else {
            //第二次通知
            if( call != null ) call.accept( true );
        }
    }
}
